package com.example.whatrubbish.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.whatrubbish.model.User;
import com.example.whatrubbish.service.UserService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.HttpRequest;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.PageBean;
import com.example.whatrubbish.vo.UserWithIntegration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

//    private final static String APPID = "wx7819bc383f3a963f";
//    private final static String AppSecret = "5884b27d997828cd20b9295f4645ef42";

    private final static String APPID = "wx18d2543ed7413a41";
    private final static String AppSecret = "153cab24771fd0a6a28125c4fd7114de";

    @Autowired
    UserService userService;

    @RequestMapping("/getOpenId")
    public String userLogin(String code){
        Object obj = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", "appid="+ APPID +"&secret="+AppSecret+"&js_code="+code+"&grant_type=authorization_code");
        JSONObject json = JSON.parseObject(obj.toString());
        return json.get("openid").toString();
    }

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(@RequestBody User user){
        JsonResult jsonResult;
        try{
            UserWithIntegration user1 = userService.findByPrimaryKey(user.getUserId());
            if(user1.getUserId()==null){
                userService.insertSelective(user);
            }else{
                userService.updateByPrimaryKey(user);
            }
            user1 = userService.findByPrimaryKey(user.getUserId());
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", user1);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/select")
    public JsonResult selectUser(HttpServletRequest request, HttpServletResponse response){
        JsonResult jsonResult;
        try {
            Integer pageIndex = request.getParameter("pageIndex")!=null? request.getParameter("pageIndex")!=""? Integer.parseInt(request.getParameter("pageIndex")) : null : null;
            Integer pageSize = request.getParameter("pageSize")!=null? request.getParameter("pageSize")!=""? Integer.parseInt(request.getParameter("pageSize")) : null : null;
            Criteria criteria = new Criteria();
            PageBean<UserWithIntegration> pageBean = new PageBean();
            if(pageIndex != null && pageSize != null){
                pageBean.setPageIndex(pageIndex);
                pageBean.setPageSize(pageSize);
                criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
                criteria.addExtField("pageSize", pageBean.getPageSize());
            }
            criteria.addExtField("userName", request.getParameter("userName")!=null?request.getParameter("userName")!=""? "%"+request.getParameter("userName")+"%" : null : null);
            List<UserWithIntegration> list = userService.selectSearchRecord(criteria);
            Long total = userService.selectSearchRecordCount(criteria);
            pageBean.setTotalCount(total);
            pageBean.setResultList(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }
}
